const Controller = require("egg").Controller
// 雪花算法
const snowflake = require("../../utils/snowflake")
class MainController extends Controller{
    async Login(){
        const {username,password} = this.ctx.request.query
        const sql = `SELECT username from user where username=? and password=?`
        let res = await this.app.mysql.query(sql,[username,password])
        if(res.length>0){
            let openId = new Date().getTime()
            this.ctx.session.openId=openId
            let LoginId = openId+"l"+(Math.random()*10).toFixed("5");
            this.ctx.session.LoginId=LoginId
            this.ctx.cookies.set("ID",LoginId,{
                maxAge:60*60*60*24,
                path:"/",
                encrypt: true, // 加密传输
            })
           
            this.ctx.body={'data':'登录成功','openId':openId}
        }else{
            this.ctx.body={'data':"登录失败"}
        }
    }
// 免密登录
    async checkLogin(){
        const {ctx} = this
        // 用户登录id
        const ID = ctx.cookies.get("ID", {
            encrypt: true,
          })
        const LoginId = ctx.session.LoginId
    
        if((ID||LoginId)&&(ID==LoginId)){
            ctx.body={"data":"已登录","state":true}
        }else{
            ctx.body={"data":"未登录","state":false}
        }
    }

    //后台文章分类信息
async getTypeInfo(){
    const resType = await this.app.mysql.select('type')
    this.ctx.body={data:resType,state:true}
}   

// 添加文章
async addArticle(){
    const {article_data} = this.ctx.request.body
    let id = new Date().getTime()
    article_data.id=id 
    article_data.view_count=0
    let res =  await this.app.mysql.insert("article",article_data)
    let effect_Row = res.affectedRows === 1
    if(effect_Row){
        this.ctx.body={data:"添加成功",state:true,insertId:id }
    }else{
        this.ctx.body={data:"添加失败",state:false}
    }
   
}
async updateArticle(){
    const {article_data} = this.ctx.request.body
    article_data.create_time=new Date(article_data.create_time)
    const res =await  this.app.mysql.update("article",article_data)
    const updateSuccess = res.affectedRows===1
    if(updateSuccess){
        this.ctx.body={data:"更新成功",state:true,insertId:article_data.id }
    }else{
        this.ctx.body={data:"更新失败",state:false}
    }
}


async getArticleList(){
    const {app,ctx} = this
    let sql=`SELECT a.id,a.title,
    a.article_content
    ,a.introduce,a.create_time
    ,a.view_count,type.type_name
     FROM article as a left join type ON a.type_id = type.id order by a.create_time`;
    let res = await app.mysql.query(sql)
    ctx.body={data:res}
}

async deleteArticle(){
    const {ctx,app} = this
    let id = ctx.request.query.id
    let res = await app.mysql.delete("article",{id:id})
    let success= res.affectedRows==1
    if(success){
        ctx.body={data:"删除成功",state:true}
    }else{
        ctx.body={data:"删除失败",state:false}
    }
    
}


async getArticleById(){
    const {app,ctx} = this
    let id = ctx.request.query.id
    let sql=`SELECT a.id,a.title,
    a.article_content
    ,a.introduce,a.create_time
    ,a.view_count,type.type_name
     FROM article as a left join type ON a.type_id = type.id where a.id=${id}`;
    let res = await app.mysql.query(sql)
    ctx.body={data:res}     
}

async logout(){
    const {app,ctx} =this
    ctx.session=null
    ctx.cookies.set('ID', null);
    ctx.status = 204;

    ctx.body={data:"退出成功",state:true}
}

}

module.exports =  MainController

